package org.dromara.system.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.BeanCopyUtils;
import org.dromara.system.mapper.SysUserMapper;
import org.dromara.system.pojo.bo.SysUserBO;
import org.dromara.system.pojo.entity.SysUserDO;
import org.dromara.system.pojo.qo.SysUserQO;
import org.dromara.system.pojo.vo.SysUserVO;
import org.dromara.system.service.SysUserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户信息表 服务层实现。
 *
 * @author mkj
 * @since 2024-07-21
 */
@RequiredArgsConstructor
@Service
public class SysUserServiceImpl implements SysUserService {

    private final SysUserMapper sysUserMapper;

    @Override
    public boolean add(SysUserBO sysUserBO) {
        SysUserDO sysUserDO = BeanCopyUtils.copy(sysUserBO, SysUserDO.class);
        return sysUserMapper.insert(sysUserDO) > 0;
    }

    @Override
    public boolean remove(Long id) {
        return sysUserMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(SysUserBO sysUserBO) {
        SysUserDO sysUserDO = BeanCopyUtils.copy(sysUserBO, SysUserDO.class);
        return sysUserMapper.update(sysUserDO) > 0;
    }

    @Override
    public List<SysUserVO> list(SysUserQO sysUserQO) {
        SysUserDO sysUserDO = BeanCopyUtils.copy(sysUserQO, SysUserDO.class);
        QueryWrapper queryWrapper = QueryWrapper.create(sysUserDO);
        return sysUserMapper.selectListByQueryAs(queryWrapper, SysUserVO.class);
    }

    @Override
    public SysUserVO getInfo(Long id) {
        SysUserDO sysUserDO = sysUserMapper.selectOneById(id);
        if (sysUserDO == null) {
            return null;
        }
        return BeanCopyUtils.copy(sysUserDO, SysUserVO.class);
    }

    @Override
    public Page<SysUserVO> page(SysUserQO page) {
        return sysUserMapper.paginateAs(page.getPageNumber(), page.getPageSize(), QueryWrapper.create(page), SysUserVO.class);
    }

}

